package P012_SerializationConstructionFinal;

import P011_ExplicitDeclarationUID.SerializationUtils;
import org.junit.Test;

import java.io.Serializable;

/**
 * Desc:&emsp;&ensp; 避免用序列化类在构造函数中为不变量赋值
 *
 * @author 李渊&emsp;liyuan7@yusys.com.cn
 * @version 1.0
 * @since V4.3&emsp;2022/2/24
 */
public class SerializationConstructionFinal {

    private static class Person implements Serializable {
        private static final long serialVersionUID = 1L;
        private final String name;

        public Person(){
//            name = "张三";
            name = "李四";
        }
        public String getName() {
            return name;
        }
    }

    @Test
    public void TestProducer() {
        Person person = new Person();
        //序列化后改变构造函数中的name，然后反序列化
        SerializationUtils.writeObject(person);
    }

    @Test
    public void TestConsumer() {
        //反序列化
        Person p = (Person) SerializationUtils.readObject();
        System.out.println("name=" + p.getName());
    }
}


